﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Collector
{
    public partial class Setting : Form
    {
        //private GlobalConfigure globalCfg;
        public Setting()
        {
            InitializeComponent();
            this.ControlBox = false;
            comboBox_TestMode.Items.Clear();
            for (int i = 0; i < FileConfig.testMode_Info.GetLength(0); i++)
            {
                string info = FileConfig.testMode_Info[i].mode;
                comboBox_TestMode.Items.Add(info);
            }
            checkBox_InputSn.Checked = FileConfig.WritePCBSN();
            checkBox_InputFreq.Checked = FileConfig.WriteFixedFrequency();
            checkBox_ResetAir.Checked = FileConfig.AirResetTest();
            checkBox_LedTest.Checked = FileConfig.LedTest();
            checkBox_PulsePreTest.Checked = FileConfig.PulsePressureTest();
            checkBox_AirPreTest.Checked = FileConfig.AirPressureTest();
            checkBox_SFT.Checked = FileConfig.PulseSFTTest();
            checkBox_FFT.Checked = FileConfig.PulseFFTTest();
            checkBox_Consecutive.Checked = FileConfig.PulseConsecutiveTest();
            checkBox_ThreeBody.Checked = FileConfig.PulseThreeBodyTest_Subsection();
            checkBox_ThreeBodyContinuous.Checked = FileConfig.PulseThreeBodyTest_Continuous();
            checkBox_Subsection.Checked = FileConfig.PulseSubsectionTest();
            comboBox_TestMode.SelectedIndex = FileConfig.CommonParam.iSelectedTestmode;
            
            //comboBox_TestMode.Enabled = !(FileConfig.CommonParam.bLockTestMode || FileConfig.CommonParam.connectServer);
            if (FileConfig.CommonParam.iSelectedTestmode == 0)
                checkBox_InputSn.Enabled = true;

            reFlashUI();
            //UpdatePortInfo();
            //AppEvent.RegistEvent(EventId.UpdateInfo, OnUpdateInputSn);
        }

        private void reFlashUI()
        {
            int index = comboBox_TestMode.SelectedIndex;
            FileConfig.CommonParam.iSelectedTestmode = comboBox_TestMode.SelectedIndex;
            if (index < 0)
            {
                return;
            }
            //AppEvent.CallEvent(this, new AppEventArgs(EventId.UpdateInfo, 0, EventState.TestModeChange, index, null));
            AppEvent.CallEvent(this, new AppEventArgs(EventId.UpdateInfo, 0, EventState.UI, comboBox_TestMode.Text, index));

        }

        private void button_PlatformChange(object sender, EventArgs e)
        {
            //FileConfig.CommonParam.iSelectedTestmode = comboBox_TestMode.SelectedIndex;
            //if (FileConfig.CommonParam.iSelectedTestmode == 0)
            //    checkBox_InputSn.Enabled = true;
            //if (FileConfig.CommonParam.iSelectedTestmode == 1)
            //    checkBox_InputSn.Enabled = false;

            reFlashUI();
        }
        private void InputSn_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox_InputSn.Checked)
                this.checkBox_FFT.Checked = true;
        }
        //InputSn_CheckedChanged

        // Button Click
        private void button_OK_Click(object sender, EventArgs e)
        {
            FileConfig.testItem_Info.dictParams["WritePCBSN"] = checkBox_InputSn.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulseSFTTest"] = checkBox_SFT.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulseFFTTest"] = checkBox_FFT.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["WriteFixedFrequency"] = checkBox_InputFreq.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["AirResetTest"] = checkBox_ResetAir.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["LedTest"] = checkBox_LedTest.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulsePressureTest"] = checkBox_PulsePreTest.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["AirPressureTest"] = checkBox_AirPreTest.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulseConsecutiveTest"] = checkBox_Consecutive.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulseThreeBodyTest"] = checkBox_ThreeBody.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulseThreeBodyContinuousTest"] = checkBox_ThreeBodyContinuous.Checked ? "1" : "0";
            FileConfig.testItem_Info.dictParams["PulseSubsectionTest"] = checkBox_Subsection.Checked ? "1" : "0";

            // Common
            FileConfig.CommonParam.AllowSavePulseData = checkBox_SaveLog.Checked ? 1 : 0;

            string ConfigFilePath = Application.StartupPath + "\\Setting.config";
            FileConfig.SaveCommonConfig(ConfigFilePath);
            int index = comboBox_TestMode.SelectedIndex;

            AppEvent.CallEvent(this, new AppEventArgs(EventId.UpdateInfo, 0, EventState.UI, comboBox_TestMode.Text, index));
            this.Close();
        }

        private void Setting_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
    }
}

